package com.moxiao.yfjh.module.blind.dal.mysql.background;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.mybatis.core.mapper.BaseMapperX;
import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.blind.controller.admin.background.vo.BackgroundExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.background.vo.BackgroundPageReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.background.BackgroundDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 背景图管理 Mapper
 *
 * @author mx
 */
@Mapper
public interface BackgroundMapper extends BaseMapperX<BackgroundDO> {

    default PageResult<BackgroundDO> selectPage(BackgroundPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BackgroundDO>()
                .likeIfPresent(BackgroundDO::getName, reqVO.getName())
                .eqIfPresent(BackgroundDO::getUrl, reqVO.getUrl())
                .eqIfPresent(BackgroundDO::getType, reqVO.getType())
                .eqIfPresent(BackgroundDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(BackgroundDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BackgroundDO::getId));
    }

    default List<BackgroundDO> selectList(BackgroundExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<BackgroundDO>()
                .likeIfPresent(BackgroundDO::getName, reqVO.getName())
                .eqIfPresent(BackgroundDO::getUrl, reqVO.getUrl())
                .eqIfPresent(BackgroundDO::getType, reqVO.getType())
                .eqIfPresent(BackgroundDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(BackgroundDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BackgroundDO::getId));
    }

    /**
     * 随机获取一条背景图
     */
    @Select("SELECT url FROM blind_background where type=#{type} and status= 0 and deleted= 0 ORDER BY RAND() LIMIT 1")
    String getRandomBackground(@Param("type") Integer type);
}
